<template>
	<view class="order">
		<view class="order_top">
			<view class="account_info">
				<view class="padding-15">
					<my-s-tabs activeColor="#e34b0f" lineColor="#e34b0f" effect slot-title @change="changeFirst" v-model="activeTab" 
						:lineHeight='6' >
						<my-s-tab v-for="(item, index) of tabList" :key="index">{{ item.name }}</my-s-tab>
					</my-s-tabs>
				</view>
			</view>
		</view>
		<view style="height:80rpx;"></view>
		<view class="order_info" style="margin-bottom: 30rpx;">
			<view class="content" v-if="list.length">
		
					
					<view v-for="(item, index) in list" :key="index">
				
							<view class="top border fs-13">
								
								<view>订单编号:{{item.order_num}}</view>
								<view :class="item.shenhe_status==6?'gray_font':(item.shenhe_status==5?'':'red-font')">
									{{item.order_status_msg}}
								</view>
							</view>
							<view @click="orderDetailPage(item,index)">
								<view class="flex detail" v-for="(second,sIndex) in item.details" :key="sIndex"
									v-if="sIndex==0">
									<image class="order_img" :src="imgRemote+second.goods_img" mode="aspectFit" />
									<view class="order_oneline">
										<view class="">
											<view class="bold two-line">{{second.goods_name}}</view>
											
										</view>
										<view class="sku" style="text-align: right;">
											{{item.details[0].sku_guige}}  x{{item.details[0].buy_num}}
										</view>
										<view class="time" style="text-align: right;">
											<text class="fs-11">共计{{item.details.length}}款商品 合计：</text>
											<text class="red-font bold" v-if="item.pay_total>0">
											￥{{$fixed(item.pay_total)}}
												</text>
											<text class="red-font bold" v-else>￥{{$fixed(item.hj)}}</text>
										</view>
									</view>
									
								</view>
								
								
							</view>
							<view class="order_option">
								<text class="fs-13">下单时间:{{($fomartDate(item.createtime)).substr(0,10)}}</text>
								
								<view style="margin-left:auto;">
									<text class="cancel_order text_box" @click="orderDetailPage(item,index)">查看详情</text>
									
									 <text class="another_order text_box" @click="confirmReceipt(item.id,index)"
										v-if="item.shenhe_status==5||item.shenhe_status==3">确认收货</text> 
									<text class="another_order text_box" v-if="item.isPay"
										@click="nowPay(item,index)">立即支付</text>
									<text class="cancel_order text_box" v-if="item.isCancel"
										@click="cancelOrder(item.id,index)">取消订单</text>
								</view>
								
								
							
							</view>
						<!-- </view> -->
					
					
				</view>
			</view>
			<view class="bitmap" v-else>
				<image :src="none" mode="widthFix"></image>
				<view class="gray_font fs-13">
					没有订单信息哦~
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="white_b pay-method ">
				<view class="way flex_left_right">
					<text class="bold">支付方式</text>
					<text class="iconfont iconguanbi" @click="$refs.popup.close()"></text>
				</view>
				<radio-group class="radio-pay" @change="payWay">
					<view class="flex_left_right">
						<view class="align_center">
							<text class="iconfont iconweixinzhifu" style="color:#09BB07;"> </text>
							<text class="bold fs-13">微信支付</text>
						</view>
						<radio value="wxpay" checked="true" style="transform:scale(0.7)" color="#FF7133"/>
					</view>
					<view class="border-color">
					</view>
					<view class="flex_left_right remain-money">
						<view class="align_center">
							<text class="iconfont iconfeiyong" style="color:#FFB92C;"> </text>
							<text class="bold fs-13">余额支付</text>
							<text class="fs-11 gray_font"
								style="margin-left:4rpx;">(可用￥{{$fixed(addressInfo.money)}})</text>
						</view>
						<radio value="money" style="transform:scale(0.7)" color="#FF7133"
							:disabled="(orderInfo.hj)>addressInfo.money?true:false" />
					</view>
					<view class="border-color">
					
					</view>
				</radio-group>
				<view class="submit-order" @click="confirmPay">提交</view>
			</view>
		</uni-popup>
		<passkeyborad :show="show" @complte="moneyPay" @close="show=false" :price="price"></passkeyborad>
	</view>
</template>

<script>
	const app = getApp().globalData;
	import passkeyborad from '@/components/yzc-paykeyboard/yzc-paykeyboard.vue'
	const {
		imgRemote,
	} = app;
	export default {
		components: {
			passkeyborad
		},
		data() {
			return {
				imgRemote: imgRemote,
				tabList: [
					{
						name: '全部',
						id: ''
					},
					{
						name: '待付款',
						id: '1'
					},
					{
						name: '待接单',
						id: '2'
					},
					{
						name: '待配送',
						id: '4.5'
					},
					{
						name: '待自提',
						id: '3'
					},
					{
						name: '待收货',
						id: '5'
					},
					{
						name: '已完成',
						id: '6'
					}
				],
				activeTab: 0,
				search_default: true,
				list: [],
				page: 1,
				id: '',
				addressInfo: '',
				totalPrice: 0,
				feeInfo: {},
				freight: 0,
				pay_type: 'wxpay',
				orderInfo: '',
				index: '',
				show: false,
				showPay: 0,
				pay_password: '',
				price:'',
				list2:[],
				none:''
			};
		},
		onLoad(e) {
			this.none=this.$imgUrl+'assets/img/none_order.png';
			console.log(e);
			if(e.status){
				
				
				this.tabList.forEach((res,index)=>{
					if(e.status==res.id){
						this.activeTab=index;
					}
				});
				
				/* if(e.status>=5){
					this.activeTab = Number(e.status)-1;
				}else{
					this.activeTab = e.status;
				} */
				
				
				this.id = e.status;
			}
			this.orderList();
		},
		onShow() {
			this.isSetPwd();
			this.getUserInfo();
			setTimeout(() => {
				let {
					orderIndex
				} = getApp().globalData;
				console.log(orderIndex)
				if (orderIndex) {
					this.list.splice(orderIndex, 1)
				}
			}, 1000)
		},
		methods: {
			getUserInfo() {
				let params = {
					token: uni.getStorageSync('userToken')
				}
				this.$get(this.$api.userInfo, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						this.addressInfo = data.data;
					}
				})
			},
			moneyPay(e) {
				this.pay_password = e;
				this.orderPay();

			},
			confirmPay(){
			
				if (this.pay_type == 'wxpay') {
					this.orderPay();
				} else if(this.pay_type=='money'){
					if (this.showPay = 1) {
						this.price=this.orderInfo.hj;
						this.$refs.popup.close();
						this.show = true;
					} else {
						this.$showModal('是否设置支付密码', (res) => {
							uni.navigateTo({
								url: '/pages/user/setPay'
							})
						})
					}
				}else{
					this.orderPay();
				}
			},
			nowPay(item, index) {
				this.totalPrice = item.hj;
				this.freight = item.freight;
				this.index = index;
				this.orderInfo = item;
					
				this.$refs.popup.open()
			},
			orderPay() {
				let _ = this;
				uni.login({
					provider: 'weixin',
					success(res) {
						let params = {
							token: uni.getStorageSync('userToken'),
							order_id: _.orderInfo.id,
							pay_type: _.pay_type,
							code: res.code
						};
						if (_.pay_type == 'money') {
							params.pay_password = _.pay_password;
						}
						_.$get(_.$api.orderPay, params, (res1) => {
							let {
								data
							} = res1;
							if (data.code == 1) {
								if (data.data == null) {
									_.$Toast(data.msg);
									_.getAddress();
									// this.$set(_.list, index, {order_status:2});
									_.list.splice(_.index,1);

								} else {
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: data.data.timeStamp,
										nonceStr: data.data.nonceStr,
										package: data.data.package,
										signType: data.data.signType,
										paySign: data.data.paySign,
										success: function(res) {
											_.$Toast('支付成功');
											_.list[_.index].order_status = 2;

											_.getAddress();

										},
										fail: function(err) {
											_.$Toast('支付取消');

										}
									});
								}
							} else {
								_.$Toast(data.msg);
								if(_.pay_type=='money'&&data.msg=="支付密码错误"){
									uni.showModal({
										title: '',
										content: '支付密码错误,请重试',
										cancelText: '忘记密码',
										confirmColor: '#FF7133',
										success(res) {
											if (res.confirm) {
												_.show = true;
											} else if (res.cancel) {
												uni.navigateTo({
													url: '/pages/user/forgetPay'
												})
											}
										}
									})
								}
							}
							_.$refs.popup.close()
						})
					}
				})

			},
			payWay(e) {
				this.pay_type = e.detail.value;
			},
			fixed(val) {
				return Number(val).toFixed(2);
			},

			orderDetailPage(item, index) {
				uni.navigateTo({
					url: '/pages/order/orderdetail?id=' + item.order_num + '&index=' + index
				})

			},
			//初始订单请求
			orderList() {
				let params = {
					token: uni.getStorageSync('userToken'),
					order_status: this.id,
					page: this.page
				};
				this.$get(this.$api.orderIndex, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						
						this.list = this.list.concat(data.data);
					} else {
						this.$Toast(data.msg);
					}

				}, true)
			},
			changeFirst(e) {
				console.log(this.activeTab);
				this.page = 1;
				this.list = [];
				this.id = this.tabList[e].id;
				this.orderList(this.id);
			},
			cancelOrder(id, index) {
				console.log(id)
				this.$showModal('确认取消订单?', () => {
					let params = {
						token: uni.getStorageSync('userToken'),
						order_id: id
					};
					this.$get(this.$api.orderCancel, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.$Toast('取消订单成功');
							this.list.splice(index, 1);
						} else {
							this.$Toast(data.msg);
						}
					})
				})

			},
			deleteOrder(id, index) {
				this.$showModal('确认删除订单?', () => {
					let params = {
						token: uni.getStorageSync('userToken'),
						order_id: id
					};
					this.$get(this.$api.orderDel_order, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.$Toast('删除订单成功');
							this.list.splice(index, 1);
						} else {
							this.$Toast(data.msg);
						}
					})
				})

			},
			oneMoreOrder(item) {
				for (let i of item.details) {
					let params = {
						token: uni.getStorageSync('userToken'),
						sku_id: i.sku_id,
						goods_id: i.goods_id,
						buy_num: i.buy_num
					}
					this.$get(this.$api.cartAdd_cart, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.$Toast('加入购物车成功');
							this.close();
						} else {
							this.$Toast(data.msg);
						}
					})
				}
				setTimeout(() => {
					uni.switchTab({
						url: "/pages/tabar/shopcart"
					})
				}, 1000)
			},
			confirmReceipt(id, index) {
				this.$showModal('确认收货?', () => {
					let params = {
						token: uni.getStorageSync('userToken'),
						order_id: id
					};
					this.$get(this.$api.orderReceipt, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.$Toast('收货成功');
							this.list.splice(index, 1);
						} else {
							this.$Toast(data.msg);
						}
					})
				})
			},
		
		
			isSetPwd() {
				let that = this;
				that.$get(that.$api.userIsset_pay_password, {
					token: uni.getStorageSync('userToken'),
				}, (res) => {
					this.showPay = res.data.code;
				})
			},
		},
		
		onReachBottom() {
			this.page++;
			this.orderList(this.id);
		},
		
		onHide() {
			getApp().globalData.orderIndex = null;
		},
		onUnload() {
			getApp().globalData.orderIndex = null;
		}
	};
</script>

<style lang="scss" scoped>
	.order .pay-method {
		padding: 0 20rpx 30rpx;

		.way {
			height: 80rpx;
			line-height: 80rpx;
		}

		.iconfont {
			font-size: 50rpx;
		}

		.submit-order {
			background: #FF7133;
			width: 200rpx;
			color: white;
			text-align: center;
			height: 60rpx;
			border-radius: 10rpx;
			line-height: 60rpx;
			margin: 0 auto;
		}
	}

	.order .select_account {
		border-bottom: 1px solid #efefef;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		height: 65rpx;
		padding-right: 18rpx;
	}

	.order .select_account text {
		color: grey;
		display: inline-block;
		margin-left: 10rpx;
	}

	.order .select_account .uni-icons {
		color: grey !important;
	}

	.order .account_info {
		background: white;
		position: fixed;
		width: 100%;
		z-index: 2;
	}

	.order .s-tabs-nav-wrap .s-tab-nav-view {
		height: 100%;
		display: flex;
		justify-content: space-around;
	}

	.order .order_option {
		border-top: 1px solid #efefef;
		height: 96rpx;
		line-height: 96rpx;
		padding-left: 10rpx;
		width: 100%;
		display: flex;
		align-items: center;
	}

	.text_box {
		border-radius: 23rpx;
		width: 130rpx;
		display: inline-block;
		height: 46rpx;
		line-height: 46rpx;
		margin-left:10rpx;
		font-size: 26rpx;
		text-align: center;
	}

	.order .another_order {
		background: white;
		color: #FF3333;
		border: 1px solid #F33;
	}

	.order .look_logist {
		background: rgb(2, 177, 228);
		color: white;
	}

	.order .cancel_order {
		background: white;
		color: #333;
		border: 1px solid #333;
	}

	.confirm_good {
		background: red;
		color: white;
		// border: 1px solid red;
	}

	.order .order_img {
		width: 208rpx;
		height: 208rpx;
		margin-right: 30rpx;
		border-radius: 10rpx;
	}

	.sku{
		font-size: 26rpx;
		color: #999;
		margin-top: 70rpx;
	}

	.order .order_info .content>view {
		background: white;
		margin: 30rpx 30rpx 0;

		border-radius: 10rpx;
	}

	.order .order_info .content .top {
		padding: 0 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 60rpx;
	}

	.order .order_info .content .detail {
		padding: 20rpx 20rpx 0;
		// border-top: 1px solid #efefef;
	}

	.order .order_statu .mask {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .5);
	}

	.order .order_statu .operate {
		margin-top: 10rpx;
		padding: 2px 0 0;
		z-index: 9999;
		border-radius: 2px;
		position: fixed;
		right: 20rpx;
		color: $m-c;
		background: #fff;
		width: 25%;
	}

	.order .order_statu .operate>view {
		height: 64rpx;
		line-height: 64rpx;
		text-align: center;
	}

	.order .order_statu .operate .all_order {
		border-bottom: 1px solid #efefef;
	}

	.order .custom-tabs {

		// padding: 0 30rpx;
		/deep/.s-tab-nav-view {
			display: flex;
			justify-content: space-between;
		}

		/deep/ .s-tab-nav {
			padding: 0;
		}
	}

	.order .bitmap {
		text-align: center;
	}

	.order .bitmap image {
		width: 60%;
		height: 500rpx;
		margin: 200rpx 0 0;
	}

	.order .order_oneline {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	/deep/ .s-tabs .s-tab-line {
		bottom: 1rpx;
		border-radius: 10rpx;
	}

	/deep/ .s-tab-nav {
		color: #000;
	}
</style>
